package com.person.jc.web.frame;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.frame.FrameService;
import com.person.jc.service.security.TsMenuService;
import com.person.jc.service.security.TsUserService;
import com.person.jc.utils.MyMD5;

public class FrameIndexController extends Controller {
	
	public void index(){
		TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
		List<Record> menuList = TsMenuService.me.getMenuByUserId(tsUser.getUserId());
		setSessionAttr("menuList", menuList);
		List<Record> funList = TsMenuService.me.getFunByUserId(tsUser.getUserId());
		setSessionAttr("funList", funList);
		render("frame/frameIndex.jsp");
	}
	
	public void workspace(){
		render("frame/workSpace.jsp");
	}
	
	public void getSubMenuDiv(){
		String menuId = getPara("menuId");
		TsUser tsUser = getSessionAttr(Constants.LOGIN_USER);
		List<Record> menuList = TsMenuService.me.getMenuByUserId(tsUser.getUserId());;
		String menuDiv = FrameService.me.getSubMenuDiv(menuList, menuId);
		renderText(menuDiv);
	}
	
	public void updatePassword(){
		
		render("frame/updatePw.jsp");
	}
	
	public void updatePw(){
		String oldPassword = getPara("oldPassword");
        String newPassword = getPara("newPassword");
        TsUser tsUser = (TsUser)getSessionAttr(Constants.LOGIN_USER);
        String returnStr = "success";
		//原始密码输入不正确
		if(!tsUser.getPassword().equals(MyMD5.myMD5(oldPassword))){
		    returnStr = "1";
		}else{
		    newPassword = MyMD5.myMD5(newPassword);
		    TsUserService.me.updatePw(tsUser.getUserId(), newPassword);
		    tsUser.setPassword(newPassword);
		    setSessionAttr(Constants.LOGIN_USER, tsUser);
		}
		renderText(returnStr);
	}
}
